<?php
namespace app\admin\controller;
use think\Db;
use think\Controller;
use think\File;
use think\Request;
class Xcxnotes extends Controller
{
	public function _initialize()
    {
    	$this->Notes   		 = Db::name('notes');			//笔记表
		$this->NotesTopic    = Db::name('notes_topic');		//笔记话题表
		$this->NotesCategory = Db::name('notes_category');	//笔记分类表
		$this->Picture       = Db::name('picture');			//图片表
		$this->NotesPraise   = Db::name('notes_praise');	//笔记点赞表
		$this->FocusFans     = Db::name('focus_fans');		//关注/粉丝表
		
    }

/*
 * 控制器：笔记列表
 * 修改人：蒋子建
 * 时间：2018.11.13
 */
 public function notesList(){
 	$openid   = input('openid');
 	$topic_id = input('topic_id');
 	$category_id = input('category_id');
 	//查询条件
 	$map = array();
 	//分类id
 	if($category_id != 0){
		$map['category_id'] = $category_id;
	}
	//话题id
	if($topic_id != 0){
		$map['topic_id'] = $topic_id;
	}
 	//分页
 	$page = input('page')?input('page'):1;
	$pageli = 10;
	$num = $page*$pageli-$pageli;
	$total = $this->Notes->where($map)->count();
	if(is_int($total/$pageli)){
		$total = $total/$pageli;
	}else{
		$total = ceil($total/$pageli);
	}
	$notes_list = $this->Notes->where($map)->order('top_sort desc,sort desc')->limit($num,$pageli)->select();
	foreach ($notes_list as $k => $v) {
		if($v['type'] == 1){
			//图片集合
		 	$v['notes_imgs'] = $this->Picture->where('id',$v['imgs'])->colnum('path','id');
		}
		//分类信息
		$v['category']   = $this->NotesCategory->where('id',$v['category_id'])->find();
		//话题信息
		$v['topic']      = $this->NotesTopic->where('id',$v['category_id'])->find();
		$notes_list[$k]  = $v;
	}
	unset($k);
	unset($v);
	return json(['code'=>1,'msg'=>'成功','notes_list'=>$notes_list,'total'=>$total]);
 }

/*
 * 控制器：首页笔记列表
 * 修改人：蒋子建
 * 时间：2018.11.13
 */
 public function indexNotesList(){
 	$user_id  = input('user_id');
 	$openid   = input('openid');
 	$topic_id = input('topic_id');
 	$category_id = input('category_id');

 	//获取所有关注人id
 	$focusIds = $this->FocusFans->where('fans_id',$user_id)->column('focus_id');
 	//插入0是平台发布的笔记
 	array_push($focusIds,0);
 	$focusIds = implode(',',$focusIds);
 	//查询条件
 	$map = array();
	$map['user_id'] = array('in',$focusIds);
	//分页
 	$page = input('page')?input('page'):1;
	$pageli = 10;
	$num = $page*$pageli-$pageli;
	$total = $this->Notes->where($map)->count();
	if(is_int($total/$pageli)){
		$total = $total/$pageli;
	}else{
		$total = ceil($total/$pageli);
	}
	$notes_list = $this->Notes->where($map)->order('top_sort desc,sort desc')->limit($num,$pageli)->select();
	foreach ($notes_list as $k => $v) {
		if($v['type'] == 1){
			//图片集合
		 	$v['notes_imgs'] = $this->Picture->where('id',$v['imgs'])->colnum('path','id');
		}
		//分类信息
		$v['category']   = $this->NotesCategory->where('id',$v['category_id'])->find();
		//话题信息
		$v['topic']      = $this->NotesTopic->where('id',$v['category_id'])->find();
		$notes_list[$k]  = $v;
	}
	unset($k);
	unset($v);
	return json(['code'=>1,'msg'=>'成功','notes_list'=>$notes_list,'total'=>$total]);
 }

/*
 * 控制器：笔记点赞
 * 修改人：蒋子建
 * 时间：2018.11.13
 */
 public function notesPraise(){
 	$openid   = input('openid');
 	$user_id  = input('user_id');
 	$notes_id = input('notes_id');
    $map['openid']   = $openid;
    $map['user_id']  = $user_id;
    $map['notes_id'] = $notes_id;
    $map['create_time'] = time();
    $praise  = $this->NotesPraise->where($map)->find();
    if($praise){
    	$add = $this->NotesPraise->where('id',$praise['id'])->delete();
    }else{
    	$add = $this->NotesPraise->insertGetId($map);
    }
    if($add){
    	return json(['code'=>1,'msg'=>'成功']);
    }else{
    	return json(['code'=>-1,'msg'=>'失败']);
    }
 }


/*
 * 控制器：发布笔记
 * 修改人：蒋子建
 * 时间：2018.11.13
 */
 public function notesRelease(){
 	$openid  = input('openid');
 	$user_id = input('user_id');
 	$type    = input('type');
 	$info    = input('info');
 	$topic_id= input('topic_id');
 	$category_id = input('category_id');
 	//条件
    $map['openid']  = $openid;
    $map['user_id'] = $user_id;
    $map['type']    = $type;
    $map['status']  = 2;
    $map['topic_id']= $topic_id;
    $map['category_id'] = $category_id;
    $map['create_time'] = time();
    //判断类型type==2/1(视频/图文)
    if($type == 2){
    	$map['url'] = input('url');
    }else{
    	$upload = new Upload;
    	$map['imgs'] = input('imgs_id');
    }
    $add = $this->Notes->insertGetId($map);
    if($add){
    	return json(['code'=>1,'msg'=>'成功']);
    }else{
    	return json(['code'=>-1,'msg'=>'失败']);
    }
 }

// public function uploadPicture(){
//         //file是传文件的名称，这是webloader插件固定写入的。因为webloader插件会写入一个隐藏input，不信你们可以通过浏览器检查页面
//         $file  = $this->request->file('file');
//         $repath=input('post.name');
// 		$info  = $file->move(ROOT_PATH . 'public' . DS . 'upload');
// 		if($info){
// 			$filepath='/public/upload/'.date('Ymd') .'/'.$info->getFilename();
// 			$picId   =$this->model->insertGetId(['path'=>$filepath,'repath'=>input('post.name'),'status'=>1,'create_time'=>time()]);
// 			$resResult['code']=1;
// 			$resResult['id']  =$picId;
// 			$resResult['path']=$filepath;
// 			return json_encode($resResult);
// 		}else{
// 			// 上传失败获取错误信息
// 			$resResult['code']=0;
// 			$resResult['info']=$file->getError();
// 			return json($resResult);
// 		}
//     }

/*
 * 控制器：小程序上传图片返回id
 * 修改人：蒋子建
 * 时间：2018.11.13
 */
    public function uploadPicture(){
        $file = $_FILES['imgs'];  	//得到传输的数据,以数组的形式
        $name = $file['name'];      //得到文件名称，以数组的形式
        $upload_path = "public/uploads/"; //上传文件的存放路径
        //当前位置
        foreach ($name as $k=>$names){
            $type = strtolower(substr($names,strrpos($names,'.')+1));//得到文件类型，并且都转化成小写
            $allow_type = array('jpg','jpeg','gif','png'); //定义允许上传的类型
            //把非法格式的图片去除
            if (!in_array($type,$allow_type)){
                unset($name[$k]);
            }
        }
        $time = date('Ymd',time());
        if(!file_exists($upload_path.$time)){
            mkdir($upload_path.$time,0777);
        }
        $upload_path = $upload_path.$time.'/';
        $str = '';
        foreach ($name as $k=>$item){
            $type = strtolower(substr($item,strrpos($item,'.')+1));//得到文件类型，并且都转化成小写
            $f = $upload_path.time().$name[$k];
            if (move_uploaded_file($file['tmp_name'][$k],$f)){
                $mll['path'] = '/'.$f;
                $mll['create_time'] = time();
                $mll['status'] = 1;
                $app = $this->Picture->insertGetId($mll);
                $str .= ','.$app;
            }
        }
        $str = ltrim($str,',');
        return json(['code'=>1,'msg'=>'成功','data'=>$str]);
    }


/*
 * 控制器：开发测试
 * 修改人：蒋子建
 * 时间：2018.11.13
 */
 public function ceshi(){
	//获取所有关注人id
 	$focusIds = $this->FocusFans->where('fans_id',1)->column('focus_id');
 	array_push($focusIds,0);
 	$focusIds = implode(',',$focusIds);
 	dump($focusIds);die;
 }

}